package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.biz.TemplatesBiz;
import com.cxf.windex.entity.JsContents;
import com.cxf.windex.entity.Templates;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板Controller
 *
 * @author chenxf
 * @version 1.0
 * @date 2018-12-24 15:53
 **/
@Slf4j
@Api(tags = {"通用 - 模板接口"})
@RestController
@RequestMapping("/user/templates")
public class TemplatesController extends BaseRestController<TemplatesBiz, Templates> {

    @ApiOperation(value = "查询文章列表", notes = "查询文章列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "记录数", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "offset", value = "页码",  paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "orderStr", value = "排序 desc",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "range", value = "用户类型 0商家 1村长",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "文章类型",  paramType = "query", dataType = "String")
    })
    @GetMapping("/list")
    public TableResultResponse<Templates> list(
            @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
            @RequestParam(defaultValue = "desc") String orderStr, String title, String type, String range) {
        List<Templates> items = baseBiz.list(limit,offset,orderStr,title,type,range);
        PageInfo<Templates> pageInfo = new PageInfo<>(items);
        return new TableResultResponse<>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
                offset, limit,
                items);


    }

    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping( "/save")
    public ObjectRestResponse save(Templates entity) {
        baseBiz.save(entity);
        return new ObjectRestResponse().rel(true).msg("保存成功");
    }

}
